<template>
	<view>
		<view>
			<navbar title="充值中心">
			</navbar>
			<view style="background-color: #f5f5f5;">
				<view style="display: flex;align-items: center;justify-content: space-between;padding: 10px;">
					<view style="display: flex;align-items: center;">
						<u-avatar :src="userInfo.avatar" size="40"></u-avatar>
						<view style="margin-left: 5px;">
							<p style="display: flex;align-items: center;">{{userInfo.nickname}}<u-icon
									v-if="userInfo.identity=='认证'" name="/static/other/xunzhang.png" size="20" /><u-icon
									v-if="userInfo.vip>new Date().getTime()" name="/static/other/vip.png" size="18" />
							</p>
						</view>
					</view>
				</view>
				<view
					style="background-color: #d5daed;line-height: 40px;border-radius: 15px 15px 0 0 ;margin: 0 10px 0 10px;padding: 15px;">
					<p style="font-weight: 600;color: #565b6e;">会员将于{{getvip(userInfo.vip)}}过期</p>
				</view>
			</view>
			<view>
				<view style="display: flex;justify-content: space-around;padding: 15px 5px;">
					<view @click="vipcurrent=index" v-for="(item,index) in viplist"
						:class="vipcurrent==index?'actvip':''"
						style="width:100%;box-shadow: 0 0 3px 3px #f5f5f5;position: relative;border-radius: 10px;overflow: hidden;text-align: center;padding-bottom: 40px;margin: 0 5px;">
						<p
							style="font-size: 12px;background-color: red;color: white;position: absolute;padding: 5px;border-radius: 0 0 10px 0;">
							限时优惠</p>
						<p style="margin-top: 40px;">{{item.title}}</p>
						<p style="font-size: 35px;color: red;font-weight: 800;"><span
								style="font-size: 16px;">￥</span>{{item.content}}</p>
						<del style="color: #86848f;">￥{{item.content*2}}</del>
					</view>
				</view>
				<view @click="go('service')"
					style="display: flex;justify-content: space-between;margin: 10px;font-size: 14px;color:#9f9dab">
					<p>温馨提示:目前只支持支付宝付款，其他付款方式请<span style="color: red;">联系客服</span></p>
				</view>
				<button @click="pay(viplist[vipcurrent].data,viplist[vipcurrent].money,viplist[vipcurrent].give)"
					style="margin: 10px;" type="primary">立即开通</button>
				<p style="margin: 10px;">#会员特权说明 :</p>
				<u-alert type="info" style="margin: 10px;"
					description="1.文件替换\n2.所有的图片上传"></u-alert>
				<p style="font-size: 13px;text-align: center;line-height: 30px;color: #909699;">最终解释权归小二云盘官方所有</p>
			</view>
		</view>
	</view>
</template>

<script>
	import store from '@/store/index.js';
	import ase from '@/common/ar-aes/ar-aes.js'
	const db = uniCloud.database();
	export default {
		data() {
			return {
				userInfo: store.state.userInfo,
				vipcurrent: 0,
				viplist: [{
					title: "一个月会员",
					content: 2,
					data: 31,
					money: 200,
					mode: true,
					give: 1
				}, {
					title: "一年会员",
					content: 20,
					data: 366,
					money: 2000,
					mode: false,
					give: 5
				}, {
					title: "两年会员",
					content: 36,
					data: 73200,
					money: 3600,
					mode: false,
					give: 10
				}]
			};
		},
		methods: {
			goweixin() {
				plus.runtime.openURL(
					'weixin://',
					function(res) {
						console.log(res);
					}
				);
				this.copy('小二云盘', '公众号名称已复制')
			},
			getvip(timestamp) {
				if (Date.parse(new Date()) < timestamp) {
					return uni.$u.timeFormat(new Date(timestamp), 'yyyy年mm月dd日') + "到期";
				} else if (timestamp == 0) {
					return "购买会员，享专属特权"
				} else {
					return "会员已过期"
				}
			},
			pay(data, money, give) {
				if (uni.getSystemInfoSync().uniPlatform == 'web') {
					this.webpay(data, money, give);
				} else {
					this.apppay(data, money, give)
				}
			},
			webpay(data, money) {
				uni.showToast({
					icon: 'error',
					title: '网页端不支持',
					duration: 2000
				});
				window.open("http://www.x2.ink")
			},
			paySuccess(data, payRes, money) {
				var that = this
				uniCloud.callFunction({
					name: 'vip',
					data: {
						data: ase.encrypt(JSON.stringify({
							value: data,
						}), 'yclycl1314520520', '9740022097400220')
					},
					success(res) {
						that.getUserInfo().then(async res => {
							that.userInfo = res
							that.message('充值成功')
							uni.hideLoading()
							const record = await db.collection("uni-pay-orders").add({
								provider: "alipay",
								type: "会员充值",
								transaction_id: payRes.tradeno,
								user_id: that.user_id,
								description: data + "天会员",
								total_fee: money
							})
						})
					},
					fail(err) {
						that.message("充值失败", "error")
					}
				});
			},
			apppay(data, money, give) {
				var that = this;
				uniCloud.callFunction({
					name: 'getorderinfo',
					data: {
						subject: '会员充值[' + this.userInfo._id + ']',
						outTradeNo: this.randomString(false, 32),
						totalFee: money
					},
					success(res) {
						uni.requestPayment({
							provider: 'alipay',
							orderInfo: res.result.OrderInfo,
							success: function(res) {
								uni.showLoading({
									title: "正在验证中"
								})
								that.paySuccess(data, res, money)
							}
						});
					}
				})
			},
			help(title, data) {
				var that = this
				uni.showModal({
					title: title,
					content: data,
					success: function(res) {
						if (res.confirm) {
							that.go('service')
						}
					}
				});
			},
			go(path) {
				uni.navigateTo({
					url: '../' + path + '/' + path
				});
			},
		}
	}
</script>

<style lang="less">
	page {
		background-color: white;
	}

	.actvip {
		border: 1px red solid;
	}

	.active {
		width: 50%;
		line-height: 50px;
		display: flex;
		justify-content: center;
		color: #86848f;
		width: 50%;
		line-height: 50px;
		display: flex;
		justify-content: center;
		color: #86848f;
	}
</style>